package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.widget.ImageView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.AttendanceEntity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.litesuits.android.log.Log;

import butterknife.Bind;

public class AttendanceListAdapter extends CommonAdapter<AttendanceEntity> {

    public String mEns = RunTimeManager.getInstance().getlanguage();
    @Bind(R.id.attendance_timeImg)
    ImageView attendanceTimeImg;
    @Bind(R.id.attendance_locationImg)
    ImageView attendanceLocationImg;
    @Bind(R.id.attendance_qt_timeImg)
    ImageView attendanceQtTimeImg;
    @Bind(R.id.attendance_qt_locationImg)
    ImageView attendanceQtLocationImg;

    public AttendanceListAdapter(Context mContext) {
        super(mContext);
    }

    /**
     * 是否是已经签到
     *
     * @param t
     * @return 2016-5-11
     */
    private boolean hasCheckIn(AttendanceEntity t) {
        return StringUtils.isNotBlank(t.getQdsj())
                || StringUtils.isNotBlank(t.getQdwz());
    }

    /**
     * 是否是已经签退
     *
     * @param t
     * @return 2016-5-11
     */
    private boolean hasCheckOut(AttendanceEntity t) {
        return StringUtils.isNotBlank(t.getQtsj())
                || StringUtils.isNotBlank(t.getQtwz());
    }

    @Override
    public void convert(ViewHolder h, AttendanceEntity t) {
        if ("en".endsWith(mEns)) {
            h.setText(R.id.attendance_now, isnull(t.getRq()) ? "undated" : t.getRq());
            Log.d("kaitoude", "标签是：/" + (isnull(t.getRq()) ? "暂无日期" : "xxx") + "/");
            h.setText(R.id.attendance_title, hasCheckIn(t) ? "Sign in" : "Did not sign in");
            Log.d("histroy",
                    t.getQdsj() + "11  " + t.getQdwz() + "22  " + t.getQtsj()
                            + "33  " + t.getQtwz());
            h.setText(R.id.attendance_time, isnull(t.getQdsj()) ? " " : t.getQdsj());
            h.setText(R.id.attendance_location,
                    isnull(t.getQdwz()) ? " " : t.getQdwz());

            h.setText(R.id.attendance_qt_title, hasCheckOut(t) ? "Sign out" : "Did not sign back");
        } else {
            h.setText(R.id.attendance_now, isnull(t.getRq()) ? "无日期" : t.getRq());
            Log.d("kaitoude", "标签是：/" + (isnull(t.getRq()) ? "暂无日期" : "xxx") + "/");
            h.setText(R.id.attendance_title, hasCheckIn(t) ? "签到" : "未签到");
            h.setText(R.id.attendance_time, isnull(t.getQdsj()) ? " " : t.getQdsj());
            if (isnull(t.getQdsj())){
                h.setVisibility(R.id.attendance_timeImg,4);
                h.setVisibility(R.id.attendance_locationImg,4);
            }
            h.setText(R.id.attendance_location,
                    isnull(t.getQdwz()) ? " " : t.getQdwz());

            h.setText(R.id.attendance_qt_title, hasCheckOut(t) ? "签退" : "未签退");
        }

        h.setText(R.id.attendance_qt_time,
                isnull(t.getQtsj()) ? " " : t.getQtsj());
        if (isnull(t.getQtsj())){
            h.setVisibility(R.id.attendance_qt_timeImg,4);
            h.setVisibility(R.id.attendance_qt_locationImg,4);
        }
        h.setText(R.id.attendance_qt_location,
                isnull(t.getQtwz()) ? " " : t.getQtwz());
    }

    public boolean isnull(String string) {
        if ("null".equals(string) || string == null || "".equals(string)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.attendance_item_history;
    }

}
